//
//  AIHealthScanViewController.swift
//  YLH
//
//  Created by qinjiangtao on 2024/1/11.
//

import UIKit

class AIHealthScanViewController: UIViewController {
    
    private var dataArr = [HomeListModel]()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.black]
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "AI健康扫描"
        
        getData()
    }
    
    private func getData() {
        QINetworkManager.requestJson(urlStr: "/Index/project", method: .get, parameters: [:]) { json in
            print(json)
            self.dataArr = json.arrayObject?.kj.modelArray(HomeListModel.self) ?? [HomeListModel]()
            self.addUI()
        }
    }
    
 
    private func addUI() {
        
        let bgImgView = UIImageView().then {
            $0.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT)
            $0.image = UIImage(named: "背景图61")
            $0.contentMode = .scaleAspectFill
            $0.clipsToBounds = true
        }
        view.addSubview(bgImgView)
        
        guard dataArr.count > 6 else {
            return
        }
        
        let model0 = dataArr[0]
        let view1 = AIHealthScanChildView().then({
            $0.frame = CGRect(x: SCREEN_WIDTH - 170, y: NAV_HEIGHT + 10, width: 162, height: 72)
            $0.model = model0
        })
        view.addSubview(view1)
        
        let model1 = dataArr[1]
        let view2 = AIHealthScanChildView().then({
            $0.frame = CGRect(x: SCREEN_WIDTH / 2 - 40, y: NAV_HEIGHT + 116, width: 162, height: 72)
            $0.model = model1
        })
        view.addSubview(view2)
        
        let model2 = dataArr[2]
        let view3 = AIHealthScanChildView().then({
            $0.frame = CGRect(x: 15, y: NAV_HEIGHT + 244, width: 162, height: 72)
            $0.model = model2
        })
        view.addSubview(view3)
        
        let model3 = dataArr[3]
        let view4 = AIHealthScanChildView().then({
            $0.frame = CGRect(x: SCREEN_WIDTH - 170, y: NAV_HEIGHT + 276, width: 162, height: 72)
            $0.model = model3
        })
        view.addSubview(view4)
        
        let model4 = dataArr[4]
        let view5 = AIHealthScanChildView().then({
            $0.frame = CGRect(x: 34, y: NAV_HEIGHT + 377, width: 162, height: 72)
            $0.model = model4
        })
        view.addSubview(view5)
        
        let model5 = dataArr[5]
        let view6 = AIHealthScanChildView().then({
            $0.frame = CGRect(x: SCREEN_WIDTH - 170, y: NAV_HEIGHT + 440, width: 162, height: 72)
            $0.model = model5
        })
        view.addSubview(view6)
        
        let model6 = dataArr[6]
        let view7 = AIHealthScanChildView().then({
            $0.frame = CGRect(x: 80, y: SCREEN_HEIGHT - 190, width: 162, height: 72)
            $0.model = model6
        })
        view.addSubview(view7)
    }
    

}


class AIHealthScanChildView: UIView {
    
    private var imgView = UIImageView()
    private var titleL = UILabel()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
//        self.backgroundColor = .white
        addUI()
    }
    
    private func addUI() {
        
        imgView = UIImageView().then {
            $0.frame = CGRect(x: 0, y: 0, width: 72, height: 72)
            $0.image = UIImage(named: "")
            $0.contentMode = .scaleAspectFill
            $0.clipsToBounds = true
            $0.addRoundedCorners(withRadius: 36)
        }
        self.addSubview(imgView)
        
        titleL = UILabel().then({
            $0.frame = CGRect(x: 72, y: 50, width: 90, height: 20)
            $0.text = ""
            $0.textColor = MainColor
            $0.font = SYSFONT(ofSize: 15)
        })
        self.addSubview(titleL)
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(tapClick))
        self.addGestureRecognizer(tap)
        
    }
    
    var model = HomeListModel() {
        didSet {
            titleL.text = model.name
            imgView.kf.setImage(with: URL(string: "\(IMAGE_URL)\(model.icon_image)"), placeholder: UIImage(named: model.name))
        }
    }
    
    @objc private func tapClick() {
        if model.name == "营养健康" {
            let ctl = NutritionAndHealthViewController()
            ctl.listModel = model
            navigationController?.pushViewController(ctl, animated: true)
        } else if model.name == "面容健康" {
            let ctl = BeautyRecommViewController()
            navigationController?.pushViewController(ctl, animated: true)
//            let ctl = uploadImgViewController()
//            ctl.typeStr = model.name
//            ctl.model = model
//            self.viewController().navigationController?.pushViewController(ctl, animated: true)
        } else if model.name == "幸福感" {
            let ctl = QuestionnaireSurveyViewController()
            ctl.code = 106
            self.navigationController?.pushViewController(ctl, animated: true)
        } else if model.name == "生活方式" {
            let ctl = QuestionnaireSurveyViewController()
            ctl.code = 107
            self.navigationController?.pushViewController(ctl, animated: true)
        } else if model.name == "医疗报告" || model.name == "居家检测" {
            let ctl = uploadImgViewController()
            ctl.typeStr = model.name
            ctl.model = model
            self.viewController().navigationController?.pushViewController(ctl, animated: true)
        } else {
            ShowHUD(str: "功能开发中")
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
